/*
@STI LICENSE
(C) 2006-2008 Universit�t Innsbruck.

This file is part of myOntology.

myOntologyis free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

myOntologyis distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with myOntology.  If not, see <http://www.gnu.org/licenses/>.

@STI LICENSE */
package view.HTMLUIcomponents;

import java.util.ArrayList;
import java.util.Iterator;

import model.Labels;
import model.Vocabulary;
import model.dataContainer.TranslationContainer;

import org.apache.commons.lang.StringEscapeUtils;
import org.openrdf.model.URI;

import controller.HelperFunctions;
import controller.elementMapper.Mapper;


public class Translations extends AbstractComponent {
	

	public static final String CSSIDFIELD = "divtranslations";
	public static final String CSSIDCONTENT = "divtranslationscontent";
	public static final String NAMEFORMELEMENT = "translationEntry";

	public static final String FOCUSIDOFEDITPANEL = NAMEFORMELEMENT + "_sel";

	public static String getEmptyFieldContainer() {

		String ret = AbstractComponent.getEmptyFieldContainer( CSSIDFIELD );
		
		return ret;
	}
	
	public static String getLinkToShowSecondaryFieldInEditMode( boolean shownavigationInEditField ) {
				
		return getLinkToShowSecondaryFieldInEditMode(CSSIDFIELD, shownavigationInEditField, Labels.TRANSLATIONS.getNamePlural());
	}
	
	/**
	 * Returns the field in view mode.
	 * @param id
	 * @param entry
	 * @return
	 */
	public static String getFieldInViewMode(  URI uri, boolean showNavigation, boolean showNavigationInEditMode ) throws Exception {
		String ret = "";

		ret += getTitleContainer( model.Labels.TRANSLATIONS, false, true );
		ret += getFormContainer( getFormInViewMode( uri, showNavigation, showNavigationInEditMode ), CSSIDCONTENT );

		// The field is printed in <div id=" fieldIdentifier "> and hence all its components can be accessed with this
		ret = getFieldContainer( ret, CSSIDFIELD );
		
		return ret;
	}
	
	/**
	 * Returns the field in view mode.
	 * @param id
	 * @param entry
	 * @return
	 */
	public static String getFieldInEditMode(  URI uri, String entry, ArrayList<Exception> exceptions, boolean showNavigation ) throws Exception {
		String ret = "";
		
		ret += getContentInEditMode(uri, entry, exceptions, showNavigation);

		// The field is printed in <div id=" fieldIdentifier "> and hence all its components can be accessed with this
		ret = getFieldContainer( ret, CSSIDFIELD );
		
		return ret;
	}

	public static String getContentInEditMode(  URI uri, String entry, ArrayList<Exception> exceptions, boolean showNavigation ) throws Exception {
		String ret = "";
		
		ret += getTitleContainer( model.Labels.TRANSLATIONS, false, true );
		ret += getFormContainer( getFormInEditMode( uri, entry, exceptions, showNavigation ), CSSIDCONTENT );
		
		return ret;
	}
	
	public static String getFormInViewMode( URI uri, boolean showNavigation, boolean showNavigationInEditMode ) throws Exception {

		String ret = "";	

		java.util.ArrayList<TranslationContainer> list = Mapper.selectTranslation( uri );
		String content = getTranslationHTMLList(list);		

		ret = renderViewForm( 
				content, 
				Mapper.selectHasVersionDateTimeString(uri),
				CSSIDCONTENT, 
				showNavigation,
				showNavigationInEditMode );
		
		return ret;
	}
	
	private static String getTranslationHTMLList( java.util.ArrayList<TranslationContainer> entry ) {
		
		String ret = "";
		
		
		if( entry==null ) {
			ret += NOTHINGSELECTED;
		}
		else {			
			Iterator<TranslationContainer> iter = entry.iterator();
			while( iter.hasNext() ) {
				ret += StringEscapeUtils.escapeHtml( iter.next().getTranslationString() ) + " ";
			}
		}
		
		return ret.trim();	
	}
	

	
	public static String getFormInEditMode( URI uri, String entry, ArrayList<Exception> exceptions, boolean showNavigation ) throws Exception {
		
		String ret = "";

		String actualVersionContent = NOTHINGSELECTED;
		String actualTimestamp = Mapper.selectHasVersionDateTimeString(uri);
		
		if( uri!=null ) {

			ArrayList<TranslationContainer> tmp = Mapper.selectTranslation( uri );
			
			if( tmp!=null ) {
				actualVersionContent = getTranslationHTMLList( tmp );
			}
		}
		
		if(entry==null)
			entry="";
		
		
		// #########################################
		
		String selectedContent = 
			"	<div id=\"" + NAMEFORMELEMENT + "_act\" >"; // WITHOUT "\n" !!!
	
		// the actual values	
		ArrayList<TranslationContainer> arr = HelperFunctions.extractTranslations(entry);
		if( arr!=null ) {
			for( int i = 0; i<arr.size(); i++ ){
				
				selectedContent += 
					getSelectedElementHTML( arr.get(i).getTranslationString(), arr.get(i).getTranslationString(), "", NAMEFORMELEMENT+ "_act", NAMEFORMELEMENT + "_deletebuttonid" ) + " ";
				
			}
		}
		
		selectedContent +=
			"	</div>\n";

		// #########################################
		// #########################################
		
		
		String addContent = 
			"	<div>\n";

		addContent += getDeleteButton(NAMEFORMELEMENT);
	
		addContent +=
			"		<div class=\"addcontainer\">\n";
		addContent +=
			"			<input tabindex=\"\" style=\"width:150px;\" id=\"" + NAMEFORMELEMENT + "_sel\" name=\"" + NAMEFORMELEMENT + "_sel\" value=\"\" type=\"text\" " +
					"onblur=\"onkeyup();\" onkeyup=\"if( validateTranslation(this,document.getElementById('" + NAMEFORMELEMENT + "_errors'))==true) document.getElementById('" + NAMEFORMELEMENT + "_addbutton').disabled=false; else document.getElementById('" + NAMEFORMELEMENT + "_addbutton').disabled=true; \"" +
					"onkeypress=\"if((event.keyCode==13)){document.getElementById('"+ NAMEFORMELEMENT +"_addbutton').click();}\" " +
					">\n";
		addContent += getLanguageSelectField();		
		addContent +=
			"			<input style=\"\" disabled=\"true\" type=\"button\" id=\"" + NAMEFORMELEMENT + "_addbutton\" onClick=\"if(validateTranslation(document.getElementById('" + NAMEFORMELEMENT + "_sel'),document.getElementById('" + NAMEFORMELEMENT + "_errors'))==true) {addToSelectedElements('"+ NAMEFORMELEMENT +"_act','"+NAMEFORMELEMENT+"',document.getElementById('" + NAMEFORMELEMENT + "_sel').value+';' + document.getElementById('language_list').options[document.getElementById('language_list').selectedIndex].value,'',document.getElementById('" + NAMEFORMELEMENT + "_sel').value+';' + document.getElementById('language_list').options[document.getElementById('language_list').selectedIndex].value,'"+NAMEFORMELEMENT + "_deletebuttonid' );}document.getElementById('" + NAMEFORMELEMENT + "_sel').focus(); document.getElementById('" + NAMEFORMELEMENT + "_sel').value='';this.disabled='true'\" value=\"add\" />";
		addContent += 
			"			<div class=\"inputhint\">Insert a single word in an other language which describes the element.</div>";
		addContent +=
			"		</div>\n";

		
		addContent +=
			"	</div>\n";
		
		
		addContent += 
			"	<div class=\"clearer\">&nbsp;</div>";

			// #########################################
			// #########################################

		
		String content = selectedContent + addContent;
		
		// the actual values (again; this field is filled / decreased with DHTML but has to be "initialized")
		content +=
		"<input type=\"hidden\" name=\"" + NAMEFORMELEMENT + "\" value=\"" + entry + "\" />\n";

		ret = renderEditForm(
			Labels.TRANSLATIONS, 
			content,
			exceptions, 
			actualVersionContent, 
			actualTimestamp,
			CSSIDCONTENT, 
			NAMEFORMELEMENT, 
			showNavigation );

		return ret;
	}
	
	

	public static String getLanguageSelectField () {
		String ret = "";
		ret += 
			"<select size=\"1\" name=\"language_list\" id=\"language_list\" value=\"\" class=\"rightselectbox\" style=\"width:80px;\"" +
				"onkeypress=\"if((event.keyCode==13)){document.getElementById('"+ NAMEFORMELEMENT +"_addbutton').click();}\" " +
			">\n";
		ret += "	<option value=\"" + Vocabulary.GERMAN +  "\">german</option>\n";     
		ret += "	<option value=\"" + Vocabulary.ITALIAN +  "\">italian</option>\n";   
		ret += "	<option value=\"" + Vocabulary.FRENCH +  "\">french</option>\n";   
		ret += "	<option value=\"" + Vocabulary.SPANISH +  "\">spanish</option>\n";   
		ret += "	<option value=\"" + Vocabulary.CHINESE +  "\">chinese</option>\n";  
		ret += "	<option value=\"" + Vocabulary.RUSSIAN +  "\">russian</option>\n";
		ret += "	<option value=\"" + Vocabulary.JAPANESE +  "\">japanese</option>\n";   
		ret += "</select>";
		return ret;
	}

}
